﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;

public partial class Default3 : System.Web.UI.Page
{
	string strCon = System.Configuration.ConfigurationManager.ConnectionStrings["xxnewsConnectionString"].ConnectionString;
	SqlConnection sqlcon;
	SqlCommand sqlcom;
	string type = string.Empty;
	protected void Page_Load(object sender, EventArgs e)
	{
		if (Session["pn"] == "" || Session["pn"] == null)
		{
			Response.Redirect("default.aspx");
		}
		if (Session["pngeren"] == "" || Session["pngeren"] == null)
		{
			Response.Redirect("default.aspx");
		}
		if (Session["pn"].ToString().Contains("材料科") == false && Session["pn"].ToString().Contains("设备科") == false)
		{
			Response.Redirect("default.aspx?lb=主卡明细&type=" + Request["type"]);
		}
		if (!IsPostBack)
		{
			this.cbType1.Checked = string.IsNullOrEmpty(Request["type"]) || Request["type"] == "0";
			this.cbType2.Checked = string.IsNullOrEmpty(Request["type"]) || Request["type"] == "1";
			g_bind();
		}
	}
	private void CheckRequest()
	{
		if (this.cbType1.Checked == this.cbType2.Checked || this.cbType1.Checked)
		{
			this.ddlDW0.SelectedValue = string.Empty;
			this.txtCardNum0.Text = string.Empty;
			this.txtCarNum0.Text = string.Empty;
			this.txtOwner0.Text = string.Empty;
			this.ddlDW0.Enabled = false;
			this.txtCardNum0.Enabled = false;
			this.txtCarNum0.Enabled = false;
			this.txtOwner0.Enabled = false;
			this.GridView1.Columns[4].Visible = false;
			this.GridView1.Columns[5].Visible = false;
			this.GridView1.Columns[6].Visible = false;
			this.GridView1.Columns[7].Visible = false;
			this.GridView1.Columns[8].HeaderText = this.cbType1.Checked == this.cbType2.Checked ? "发生金额（元）" : "充值金额（元）";
			this.lType1.Text = this.cbType1.Checked == this.cbType2.Checked ? "充值分拨" : "充值";
		}
		else
		{
			this.ddlDW0.Enabled = true;
			this.txtCardNum0.Enabled = true;
			this.txtCarNum0.Enabled = true;
			this.txtOwner0.Enabled = true;
			this.GridView1.Columns[4].Visible = true;
			this.GridView1.Columns[5].Visible = true;
			this.GridView1.Columns[6].Visible = true;
			this.GridView1.Columns[7].Visible = true;
			this.GridView1.Columns[8].HeaderText = "分拨金额（元）";
			this.lType1.Text = "分拨";
		}
		this.lType2.Text = this.lType1.Text;
		this.Title = string.Format("油卡管理 - 主卡{0}记录明细", this.lType1.Text);
	}
	protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
	{
		if (e.Row.RowType == DataControlRowType.DataRow)
		{
			e.Row.Attributes.Add("onmouseover", "c=this.style.backgroundColor;this.style.backgroundColor='#D1DDF1'");
			e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=c");
			if (e.Row.RowState == DataControlRowState.Normal || e.Row.RowState == DataControlRowState.Alternate)
			{
				decimal amount = (decimal)this.GridView1.DataKeys[e.Row.RowIndex]["amount"];
				decimal balance = (decimal)this.GridView1.DataKeys[e.Row.RowIndex]["balance"];
				((DataControlFieldCell)e.Row.Cells[8]).ForeColor = amount >= 0 ? System.Drawing.Color.DarkGreen : System.Drawing.Color.Red;
				((DataControlFieldCell)e.Row.Cells[9]).ForeColor = balance > 10000 ? System.Drawing.Color.DarkGreen : (balance > 0 ? System.Drawing.Color.Chocolate : System.Drawing.Color.Red);
			}
		}
		if (e.Row.RowIndex != -1)
		{
			int id = e.Row.RowIndex + 1;
			e.Row.Cells[1].Text = id.ToString();
		}

	}
	protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
	{
		g_bind();
	}
	protected void ImageButton2_Click(object sender, ImageClickEventArgs e)
	{
		this.txtTime0.Text = string.Empty;
		this.txtTime1.Text = string.Empty;
		this.txtMan.Text = string.Empty;
		this.cbType1.Checked = string.IsNullOrEmpty(Request["type"]) || Request["type"] == "0";
		this.cbType2.Checked = string.IsNullOrEmpty(Request["type"]) || Request["type"] == "1";
		this.ddlDW0.SelectedValue = string.Empty;
		this.txtCardNum0.Text = string.Empty;
		this.txtCarNum0.Text = string.Empty;
		this.txtOwner0.Text = string.Empty;
		g_bind();
	}
	public void g_bind()
	{
		CheckRequest();
		Label_tiaojian.Text = "当前选择条件:";
        string countsql = @"select type,sum(amount) amount,count(distinct jgname) jgname,count(distinct bzname) bzname,count(distinct cardid)cardid from({0}) as t group by type";// with rollup
		string sqlstr = @"select m.*,isnull(c.inout,0) as type,c.jgname,c.bzname,c.cardid,c.carnum,c.cardid+'【'+ISNULL(c.carnum,'无车号')+'】' as cardnum,c.currentowner from youkaguanli_maincard_detail as m left join youkaguanli_card_detail as c on m.planid = c.planid where 1=1 ";
		if (this.cbType1.Checked != this.cbType2.Checked)
		{
			sqlstr += string.Format(" and m.planid is {0} null", this.cbType1.Checked ? string.Empty : "not");
			Label_tiaojian.Text += "类型：" + (this.cbType1.Checked ? "充值" : "分拨") + "记录；  ";
		}
		if (!string.IsNullOrEmpty(this.txtTime0.Text) || !string.IsNullOrEmpty(this.txtTime1.Text))
		{
			string start = this.txtTime0.Text;
			if (this.txtTime0.Text.CompareTo(this.txtTime1.Text) > 0 && !string.IsNullOrEmpty(this.txtTime1.Text))
			{
				this.txtTime0.Text = this.txtTime1.Text;
				this.txtTime1.Text = start;
			}
			if (!string.IsNullOrEmpty(this.txtTime0.Text))
				sqlstr += " and m.changetime>=cast('" + this.txtTime0.Text + "' as smalldatetime)";
			if (!string.IsNullOrEmpty(this.txtTime1.Text))
				sqlstr += " and m.changetime<=cast('" + this.txtTime1.Text + "' as smalldatetime)";
			Label_tiaojian.Text += "时间范围：从" + (string.IsNullOrEmpty(this.txtTime0.Text) ? "?" : this.txtTime0.Text) + "至" + (string.IsNullOrEmpty(this.txtTime1.Text) ? "?" : this.txtTime1.Text) + "；  ";
		}
		if (!string.IsNullOrEmpty(this.txtMan.Text.Trim()))
		{
			sqlstr += " and m.addname like '%" + this.txtMan.Text.Trim() + "%' ";
			Label_tiaojian.Text += "办理人：包含'" + this.txtMan.Text.Trim() + "'；  ";
		}
		if (!string.IsNullOrEmpty(this.ddlDW0.SelectedValue.Trim()))
		{
			sqlstr += " and (c.jgname='" + this.ddlDW0.SelectedItem.Text.Trim() + "' or c.bzname='" + this.ddlDW0.SelectedItem.Text.Trim() + "') ";
			Label_tiaojian.Text += "分拨车间或分拨班组：包含'" + this.ddlDW0.SelectedItem.Text.Trim() + "'；  ";
		}
		if (!string.IsNullOrEmpty(this.txtCardNum0.Text.Trim()))
		{
			sqlstr += " and c.cardid like '%" + this.txtCardNum0.Text.Trim() + "%' ";
			Label_tiaojian.Text += "卡号：包含'" + this.txtCardNum0.Text.Trim() + "'；  ";
		}
		if (!string.IsNullOrEmpty(this.txtCarNum0.Text.Trim()))
		{
			sqlstr += " and c.carnum like '%" + this.txtCarNum0.Text.Trim() + "%' ";
			Label_tiaojian.Text += "车号：包含'" + this.txtCarNum0.Text.Trim() + "'；  ";
		}
		if (!string.IsNullOrEmpty(this.txtOwner0.Text.Trim()))
		{
			sqlstr += " and c.currentowner like '%" + this.txtOwner0.Text.Trim() + "%' ";
			Label_tiaojian.Text += "持卡人：包含'" + this.txtOwner0.Text.Trim() + "'；  ";
		}
		if (Label_tiaojian.Text == "当前选择条件:") Label_tiaojian.Text = "当前选择条件:无";
		countsql = string.Format(countsql, sqlstr);
		sqlstr += " order by m.changetime desc, m.addtime desc, m.id";
		SqlDataSource1.SelectCommand = sqlstr;
		GridView1.DataSourceID = SqlDataSource1.ID;
		GridView1.DataBind();
		try
		{
			sqlcon = new SqlConnection(strCon);
			SqlDataAdapter myda1 = new SqlDataAdapter(countsql, sqlcon);
			DataSet myds1 = new DataSet();
			myda1.Fill(myds1, "count");

			if (myds1.Tables[0].Rows.Count != 0)
			{
				int intype = -1;
				int outtype = -1;
                decimal hj_amount = 0;
				for (int i = 0; i < myds1.Tables[0].Rows.Count; i++)
				{
					if ((int)myds1.Tables[0].Rows[i]["type"] == 0) intype = i;
					else outtype = i;
                    hj_amount += (decimal)myds1.Tables[0].Rows[i]["amount"];
				}
				label_zongjia.Visible = true;
				label_zongjia.Text = "当前选择条件下共计："
					+ "充值" + Server.HtmlEncode(string.Format("{0:C}", intype < 0 ? 0 : (decimal)myds1.Tables[0].Rows[intype]["amount"])) + "元，"
					+ "分拨" + Server.HtmlEncode(string.Format("{0:C}", outtype < 0 ? 0 : (decimal)myds1.Tables[0].Rows[outtype]["amount"])) + "元（"
                     + (outtype < 0 ? 0 :myds1.Tables[0].Rows[outtype]["jgname"]) + "个车间、"
                    + (outtype < 0 ? 0 :myds1.Tables[0].Rows[outtype]["bzname"]) + "个班组、"
                    + (outtype < 0 ? 0 :myds1.Tables[0].Rows[outtype]["cardid"]) + "张油卡），"
					+ "流水合计" + Server.HtmlEncode(string.Format("{0:C}", hj_amount)) + "元。";
			}
			else
			{
				label_zongjia.Visible = false;
			}
		}
		catch (SqlException)
		{
			throw;
		}
	}
	protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
	{
		g_bind();
		ImageButton1.Focus();
	}

	protected void cbType_CheckedChanged(object sender, EventArgs e)
	{
		CheckRequest();
		g_bind();
	}
}